package model.tokens;

import util.reflection.Reflection;
import model.semantics.Node;
import model.semantics.Operation;
import model.semantics.OperatorNode;

/**
 * Factory class to create nodes from tokens. Called from FunctionalToken.
 * 
 * @author Sterling Dorminey
 */
public class NodeFactory {
    public static NodeFactory instance;

    @SuppressWarnings("unchecked")
    public static <T> Node<T> createOperatorNode(int order, String[] callArgs,
            Node<T>[] values, String label) {
        Operation<T> op;
        if (order < 0) {
            op = (Operation<T>) Reflection.createInstance(callArgs[0],
                    values.length, label.trim());
        } else {
            op = (Operation<T>) Reflection.createInstance(callArgs[0],
                    label.trim());
        }

        return new OperatorNode<T>(op, values);
    }

    @SuppressWarnings("unchecked")
    public static <T> Node<T> createNode(int order, String[] callArgs,
            Node<T>[] values, String label) {
        Node<T> node = (Node<T>) Reflection.createInstance(callArgs[0],
                (Object[]) values);
        return node;
    }

    public static NodeFactory getInstance() {
        if (instance == null) {
            instance = new NodeFactory();
        }

        return instance;
    }
}
